package com.TinyBBS.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.TinyBBS.bean.Review;
import com.TinyBBS.model.GameModel;
import com.TinyBBS.model.ReviewModel;
import com.TinyBBS.util.Util;

/**
 * Servlet implementation class Reviews
 */
public class Reviews extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String uri = "";
		String error = "";
		if (request.getParameter("reviewSubmit") == null) {
			uri = "views/404Error.jsp";
		} else {
			int rating = Integer.valueOf(request.getParameter("vote"));
			String title = request.getParameter("title").trim();
			String content = request.getParameter("comment").trim();
			String userId = request.getParameter("userId").trim();
			String gameId = request.getParameter("gameId").trim();

			Review review = new Review();
			ReviewModel reviewM = new ReviewModel();
			GameModel gameM = new GameModel();

			review.setContent(content);
			review.setPostDate(Util.getCurrentTimeStamp());
			review.setTitle(title);
			review.setGame_id(Integer.valueOf(gameId));
			review.setUser_id(Integer.valueOf(userId));

			if (gameM.setRateById(gameId, rating)) {
				System.out.println("rated");
			}
			if (reviewM.insertReview(review)) {
				uri = "redirect/game?id=" + gameId;
				response.sendRedirect(uri);
			} else {
				request.getRequestDispatcher(uri).forward(request, response);
			}
		}
	}
}
